import { createSlice } from "@reduxjs/toolkit";
import { Local } from '@/utils/local'
import { User_Login, User_login_out, User_update } from "@/apis/user"
import { message } from "antd"
import type { AppDispatch } from "../index"
import type { UserLogin, Update } from "@/types/user"
import { UserGet } from "@/apis/collect"

const UserStore = createSlice({
    name: "user",
    initialState: {
        userinfo: Local.get_info() || {
            avatar: "",
            brithday: "",
            constellation: "",
            description: "",
            sex: "",
            username: "",
            _id: ""
        },
        usercollect: Local.get_collect() || {
            collect: 0,
            foods: 0
        }
    },
    reducers: {
        set_userinfo: (state, action) => {
            Local.set_info(action.payload);
            state.userinfo = action.payload;
        },
        login_out: (state) => {
            state.userinfo = {
                avatar: "",
                brithday: "",
                constellation: "",
                description: "",
                sex: "",
                username: "",
                _id: ""
            }
            Local.remove_info()
            Local.remove_collect()
        },
        update_user(state, action) {
            Local.set_info(action.payload);
            state.userinfo = action.payload;
        },
        setCollect(state, { payload }) {
            const { collect, food } = payload
            console.log(payload, 'ssss')
            if (food.length === 0 && collect.length === 0) {

                state.usercollect = {
                    collect: 0,
                    foods: 0
                }
                Local.set_collect({
                    collect: 0,
                    foods: 0
                })
                return
            }
            if (collect.length === 0 && food.length !== 0) {
                state.usercollect.collect = 0
                state.usercollect.foods = food[0].food
                Local.set_collect({
                    collect: 0,
                    foods: food[0].food
                })
                return
            }
            if (food.length === 0 && collect.length !== 0) {
                state.usercollect.foods = 0
                state.usercollect.foods = collect[0].collect
                Local.set_collect({
                    collect: collect[0].collect,
                    foods: 0
                })
                return
            }
            if (collect.length !== 0 && food.length !== 0) {
                state.usercollect = {
                    collect: collect[0].collect,
                    foods: food[0].food
                }
                Local.set_collect({
                    collect: collect[0].collect,
                    foods: food[0].food
                })
                return
            }
        }

    }
})
export const { set_userinfo, login_out, update_user, setCollect } = UserStore.actions
const axios_user_login = (data: UserLogin) => {
    return async (dispatch: AppDispatch) => {
        const res = await User_Login(data)
        const result = await UserGet(res.data._id)
        dispatch(setCollect(result.data))
        console.log(result.data, '??')
        dispatch(set_userinfo(res.data))
    }
}
const axios_user_loginOut = () => {
    return async (dispatch: AppDispatch) => {
        console.log("虚")
        const res = await User_login_out()
        console.log(res, 'dddd')
        if (res.status === 200) {
            message.success("退出成功")
            dispatch(login_out())
        } else {
            message.success("退出失败")
        }
    }
}
const axios_user_upload = (data: Update) => {
    return async (dispatch: AppDispatch) => {
        const res = await User_update(data)
        console.log(res, 'dddd')
        if (res.status === 200) {
            message.success("修改成功")
            dispatch(update_user(data))
        } else {
            message.success("修改失败")
        }
    }
}
export { axios_user_login, axios_user_loginOut, axios_user_upload }
export default UserStore.reducer;